package com.example.eat.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class RecipeDatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "recipe_manager.db";
    private static final int DATABASE_VERSION = 1; // 升级数据库版本

    // 食谱表
    public static final String TABLE_RECIPES = "recipes";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_NAME = "name";
    public static final String COLUMN_IMAGE = "image";
    public static final String COLUMN_INSTRUCTIONS = "instructions";
    public static final String COLUMN_CATEGORY = "category";
    public static final String COLUMN_USER_ID = "user_id";

    // 用户表
    public static final String TABLE_USERS = "users";
    public static final String COLUMN_USERNAME = "username";
    public static final String COLUMN_PASSWORD = "password";

    // 收藏关系表
    public static final String TABLE_FAVORITES = "favorites";
    public static final String COLUMN_FAV_USER_ID = "user_id";
    public static final String COLUMN_FAV_RECIPE_ID = "recipe_id";

    private static final String CREATE_TABLE_RECIPES = "CREATE TABLE " +
            TABLE_RECIPES + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
            COLUMN_NAME + " TEXT NOT NULL," +
            COLUMN_IMAGE + " TEXT," +
            COLUMN_INSTRUCTIONS + " TEXT NOT NULL," +
            COLUMN_CATEGORY + " TEXT NOT NULL," +
            COLUMN_USER_ID + " INTEGER NOT NULL," +
            "FOREIGN KEY (" + COLUMN_USER_ID + ") REFERENCES " +
            TABLE_USERS + "(" + COLUMN_ID + ")" +
            ")";

    private static final String CREATE_TABLE_USERS = "CREATE TABLE " +
            TABLE_USERS + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
            COLUMN_USERNAME + " TEXT NOT NULL UNIQUE," +
            COLUMN_PASSWORD + " TEXT NOT NULL" +
            ")";
    // 创建收藏关系表的SQL语句
    private static final String CREATE_TABLE_FAVORITES =
            "CREATE TABLE " + TABLE_FAVORITES + " (" +
                    COLUMN_FAV_USER_ID + " INTEGER NOT NULL, " +
                    COLUMN_FAV_RECIPE_ID + " INTEGER NOT NULL, " +
                    "PRIMARY KEY (" + COLUMN_FAV_USER_ID + ", " + COLUMN_FAV_RECIPE_ID + "), " +
                    "FOREIGN KEY (" + COLUMN_FAV_USER_ID + ") REFERENCES " + TABLE_USERS + "(" + COLUMN_ID + "), " +
                    "FOREIGN KEY (" + COLUMN_FAV_RECIPE_ID + ") REFERENCES " + TABLE_RECIPES + "(" + COLUMN_ID + ")" +
                    ")";

    public RecipeDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_USERS);
        db.execSQL(CREATE_TABLE_RECIPES);
        db.execSQL(CREATE_TABLE_FAVORITES); // 创建收藏关系表
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECIPES);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
        onCreate(db);
    }
}